package com.demo.sso.sdk.model;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.io.Serializable;

/**
 * SSO访问令牌模型
 * 
 * @author SpringCloud团队
 * @version 1.0.0
 * @since 2025-01-01
 */
public class SsoAccessToken implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 访问令牌
     */
    @JsonProperty("access_token")
    private String accessToken;

    /**
     * 令牌类型（通常为Bearer）
     */
    @JsonProperty("token_type")
    private String tokenType;

    /**
     * 令牌过期时间（秒）
     */
    @JsonProperty("expires_in")
    private Long expiresIn;

    /**
     * 刷新令牌
     */
    @JsonProperty("refresh_token")
    private String refreshToken;

    /**
     * 授权范围
     */
    @JsonProperty("scope")
    private String scope;

    /**
     * 令牌创建时间（时间戳）
     */
    @JsonProperty("create_time")
    private Long createTime;

    public SsoAccessToken() {
        this.createTime = System.currentTimeMillis();
    }

    public SsoAccessToken(String accessToken, String tokenType, Long expiresIn) {
        this();
        this.accessToken = accessToken;
        this.tokenType = tokenType;
        this.expiresIn = expiresIn;
    }

    /**
     * 判断令牌是否过期
     * 
     * @return true-已过期，false-未过期
     */
    public boolean isExpired() {
        if (expiresIn == null || createTime == null) {
            return false;
        }
        long currentTime = System.currentTimeMillis();
        return (currentTime - createTime) / 1000 >= expiresIn;
    }

    /**
     * 获取剩余有效时间（秒）
     * 
     * @return 剩余有效时间，负数表示已过期
     */
    public long getRemainingTime() {
        if (expiresIn == null || createTime == null) {
            return Long.MAX_VALUE;
        }
        long currentTime = System.currentTimeMillis();
        return expiresIn - (currentTime - createTime) / 1000;
    }

    // Getters and Setters

    public String getAccessToken() {
        return accessToken;
    }

    public void setAccessToken(String accessToken) {
        this.accessToken = accessToken;
    }

    public String getTokenType() {
        return tokenType;
    }

    public void setTokenType(String tokenType) {
        this.tokenType = tokenType;
    }

    public Long getExpiresIn() {
        return expiresIn;
    }

    public void setExpiresIn(Long expiresIn) {
        this.expiresIn = expiresIn;
    }

    public String getRefreshToken() {
        return refreshToken;
    }

    public void setRefreshToken(String refreshToken) {
        this.refreshToken = refreshToken;
    }

    public String getScope() {
        return scope;
    }

    public void setScope(String scope) {
        this.scope = scope;
    }

    public Long getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Long createTime) {
        this.createTime = createTime;
    }

    @Override
    public String toString() {
        return "SsoAccessToken{" +
                "accessToken='" + (accessToken != null ? accessToken.substring(0, Math.min(accessToken.length(), 10)) + "..." : null) + '\'' +
                ", tokenType='" + tokenType + '\'' +
                ", expiresIn=" + expiresIn +
                ", refreshToken='" + (refreshToken != null ? refreshToken.substring(0, Math.min(refreshToken.length(), 10)) + "..." : null) + '\'' +
                ", scope='" + scope + '\'' +
                ", createTime=" + createTime +
                ", isExpired=" + isExpired() +
                '}';
    }
}